<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>数组比较</title>
        <style>
            h3{
                text-align: center;
            }
            div{
                text-align: center;
            }
        </style>
    </head>
    <body>
        <h3>数组比较分类</h3>
        <div>
            <input id="s1" name="s1" type="text">
            <input id="s2" name="s2" type="text">
        </div>
        <div>
            <button onclick="fun()">匹配</button>
        </div>
        <div>
            <textarea id="ans" rows="10" cols="50" readonly></textarea>
        </div>
        <script>
            function fun(){
                let inp = document.getElementById("s1").value;
                let s1 = inp.split(',');
                inp = document.getElementById("s2").value;
                let s2 = inp.split(',');
                s1.sort();s2.sort();
                let p1 = 0,p2 = 0;
                let ans1=[],ans2=[];
                while(p1<s1.length&&p2<s2.length){
                    if(s1[p1]===s2[p2]){
                        ans1.push(s1[p1]);
                        ++p1;++p2;
                    }
                    else if(s1[p1]<s2[p2]){
                        ans2.push(s1[p1]);
                        ++p1;
                    }
                    else {
                        ans2.push(s2[p2]);
                        ++p2;
                    }
                }
                while(p1<s1.length){
                    ans2.push(s1[p1]);
                    ++p1;
                }
                while(p2<s2.length){
                    ans2.push(s2[p2]);
                    ++p2;
                }
                let str1 = "same: "+ans1[0];
                p1 = 1;p2 = 1;
                for(;p1<ans1.length;++p1) str1+=","+ans1[p1];
                let str2 = "diff: "+ans2[0];
                for(;p2<ans2.length;++p2) str2+=","+ans2[p2];
                document.getElementById("ans").innerHTML=str1+'\n'+str2;
            }
        </script>
    </body>
</html>